/**
 * TP no: 1
 * 
 * Titre du TP : simple Jointure
 *
 * Date : 02 oct 2010
 * 
 * Nom  : NGUYEN
 * Prenom : Quang-Hoang
 * 
 * Remarques :
 */
package run;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Main {
	public static void main(String args[])
	{				
		List<Character> R = new ArrayList<Character>();
		List<Character> S = new ArrayList<Character>();
		try{
			readFromFileToRelation("R.txt",R);
			readFromFileToRelation("S.txt",S);
			Collections.sort(R);
			Collections.sort(S);
			System.out.println("Jointure:");
			FileOutputStream out = new FileOutputStream("jointure.txt");
			for(Character c: R){
				if(S.contains(c)){
					System.out.print(c);
					out.write(c);
					out.write('\n');
				}		    	
		    }
			out.close();
			
		}
		catch (Exception e) {
			System.out.print(e);
		}
    	
	}
	
	static void readFromFileToRelation(String fileName, List<Character> relationName){
		try{
			
    		//read R.txt
		    DataInputStream in = new DataInputStream(new FileInputStream(fileName));
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		    String strLine;
		    int i = 0;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null){		   
		    	relationName.add(strLine.charAt(0));
		    	i++;
		    }
		       	
		    //Close the input stream
		    in.close();
    		
    	}
    	catch(Exception e){
    		System.out.println(e);
    	}
	}
}
